package com.jiutong.meipenggang.ui.activitys;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.ui.fragments.DailyActFragment;
import com.jiutong.meipenggang.ui.fragments.NewActFragment;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * @author Veken
 */
public class HuoDongActivity extends BaseActivity {


    @Bind(R.id.tv_new_act)
    TextView tvNewAct;
    @Bind(R.id.tv_daily_act)
    TextView tvDailyAct;
    @Bind(R.id.tv_act_new)
    TextView tvActNew;
    private FragmentManager supportFragmentManager;
    private FragmentTransaction ft;
    private NewActFragment newFragment;
    private DailyActFragment dailyActFragment;
    private Fragment mContent;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_huo_dong;
    }


    @Override
    protected void initView() {
        super.initView();
        mImmersionBar.fitsSystemWindows(false).statusBarDarkFont(false).init();
        supportFragmentManager = getSupportFragmentManager();
        ft = supportFragmentManager.beginTransaction();
        newFragment = new NewActFragment();
        dailyActFragment = new DailyActFragment();
        commitFragment(R.id.fl_fragment,newFragment);
        mContent = newFragment;
    }

    public void commitFragment(int layoutId, Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.replace(layoutId, fragment);
        transaction.commit();
    }

    /**
     * 切换tab页面
     *
     * @param fragment 要显示的fragment
     */
    public void switchContent(Fragment fragment) {
        if (mContent != fragment) {
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            if (!fragment.isAdded()) {
                transaction.hide(mContent).add(R.id.fl_fragment, fragment)
                        .commit();
            } else {
                transaction.hide(mContent).show(fragment)
                        .commitAllowingStateLoss();
            }
            mContent = fragment;
        }
    }



        @OnClick({R.id.tv_new_act, R.id.tv_daily_act,R.id.rl_huodong_back})
        public void onViewClicked (View view){
            switch (view.getId()) {
                case R.id.rl_huodong_back:
                    finish();
                    break;
                case R.id.tv_new_act:
                    tvNewAct.setTextColor(getResources().getColor(R.color.common_blue));
                    tvNewAct.setBackgroundResource(R.drawable.shape_bg_title_left_white);
                    tvDailyAct.setTextColor(getResources().getColor(R.color.white));
                    tvDailyAct.setBackgroundResource(R.drawable.shape_bg_title_right_blue);
                    switchContent(newFragment);
                    break;
                case R.id.tv_daily_act:
                    tvActNew.setVisibility(View.GONE);
                    tvNewAct.setTextColor(getResources().getColor(R.color.white));
                    tvNewAct.setBackgroundResource(R.drawable.shape_bg_title_left_blue);
                    tvDailyAct.setTextColor(getResources().getColor(R.color.common_blue));
                    tvDailyAct.setBackgroundResource(R.drawable.shape_bg_title_right_white);
                    switchContent(dailyActFragment);
                    break;
                default:
                    break;
            }
        }

    }
